package com.ruoyi.web.controller.business;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.HurtQuestionnaireScore;
import com.ruoyi.business.service.IHurtQuestionnaireScoreService;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtPatientQuestionnaire;
import com.ruoyi.business.service.IHurtPatientQuestionnaireService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 患者调查问卷记录Controller
 *
 * @author ruoyi
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/business/questionnaire")
public class HurtPatientQuestionnaireController extends BaseController
{
    @Autowired
    private IHurtPatientQuestionnaireService hurtPatientQuestionnaireService;

    @Autowired
    private IHurtQuestionnaireScoreService hurtQuestionnaireScoreService;

    /**
     * 查询患者调查问卷记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtPatientQuestionnaire hurtPatientQuestionnaire)
    {
        startPage();
        List<HurtPatientQuestionnaire> list = hurtPatientQuestionnaireService.selectHurtPatientQuestionnaireList(hurtPatientQuestionnaire);
        return getDataTable(list);
    }

    /**
     * 导出患者调查问卷记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:export')")
    @Log(title = "患者调查问卷记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtPatientQuestionnaire hurtPatientQuestionnaire)
    {
        List<HurtPatientQuestionnaire> list = hurtPatientQuestionnaireService.selectHurtPatientQuestionnaireAndScoreList(hurtPatientQuestionnaire);
        ExcelUtil<HurtPatientQuestionnaire> util = new ExcelUtil<HurtPatientQuestionnaire>(HurtPatientQuestionnaire.class);
        util.exportExcel(response, list, "患者调查问卷数据");
    }

    /**
     * 获取患者调查问卷记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtPatientQuestionnaireService.selectHurtPatientQuestionnaireById(id));
    }

    /**
     * 新增患者调查问卷记录
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:add')")
    @Log(title = "患者调查问卷记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtPatientQuestionnaire hurtPatientQuestionnaire)
    {
        return toAjax(hurtPatientQuestionnaireService.insertHurtPatientQuestionnaire(hurtPatientQuestionnaire));
    }

    /**
     * 修改患者调查问卷记录
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:edit')")
    @Log(title = "患者调查问卷记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtPatientQuestionnaire hurtPatientQuestionnaire)
    {
        return toAjax(hurtPatientQuestionnaireService.updateHurtPatientQuestionnaire(hurtPatientQuestionnaire));
    }

    /**
     * 删除患者调查问卷记录
     */
    @PreAuthorize("@ss.hasPermi('business:questionnaire:remove')")
    @Log(title = "患者调查问卷记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtPatientQuestionnaireService.deleteHurtPatientQuestionnaireByIds(ids));
    }

    /**
     * 添加患者调查问卷作答记录
     * @param hurtPatientQuestionnaire
     * @return
     */
    @PostMapping("/addPatientResult")
    public AjaxResult addPatientResult(@RequestBody HurtPatientQuestionnaire hurtPatientQuestionnaire){
        Long userId = SecurityUtils.getUserId();
        hurtPatientQuestionnaire.setPatientId(userId);
        int i = hurtPatientQuestionnaireService.insertPatientAnswerRecord(hurtPatientQuestionnaire);
        if (i>0){
            //计算得分
            getScore(hurtPatientQuestionnaire);
            //返回问卷作答记录id
            Map<String,Long> map=new HashMap<>();
            map.put("rid",hurtPatientQuestionnaire.getId());
            return AjaxResult.success(map);
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 计算odi得分
     * @param hurtPatientQuestionnaire
     * @return
     */
    public void getScore(HurtPatientQuestionnaire hurtPatientQuestionnaire){
        HurtQuestionnaireScore score=new HurtQuestionnaireScore();
        score.setRid(hurtPatientQuestionnaire.getId());
        score.setPatientId(hurtPatientQuestionnaire.getPatientId());
        System.out.println("==========================计算ODI==========================");
        System.out.println("hurtPatientQuestionnaire = " + hurtPatientQuestionnaire);
        //答题个数
        int count=0;
        int countSocre=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion1())){
            Integer q1 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion1());
            count++;
            countSocre=countSocre+q1;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion2())){
            Integer q2 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion2());
            count++;
            countSocre=countSocre+q2;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion3())){
            Integer q3 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion3());
            count++;
            countSocre=countSocre+q3;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion4())){
            Integer q4 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion4());
            count++;
            countSocre=countSocre+q4;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion5())){
            Integer q5 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion5());
            count++;
            countSocre=countSocre+q5;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion6())){
            Integer q6 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion6());
            count++;
            countSocre=countSocre+q6;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion7())){
            Integer q7 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion7());
            count++;
            countSocre=countSocre+q7;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion8())){
            Integer q8 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion8());
            count++;
            countSocre=countSocre+q8;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion9())){
            Integer q9 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion9());
            count++;
            countSocre=countSocre+q9;
        }

        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion10())){
            Integer q10 = Integer.valueOf(hurtPatientQuestionnaire.getQuestion10());
            count++;
            countSocre=countSocre+q10;
        }
        System.out.println("答题分数:"+countSocre);
        System.out.println("答题数量:"+count);
        //总分＝（所得分数/5×回答的问题数）×100%
        BigDecimal allSum=new BigDecimal(countSocre);
        String odi = allSum.multiply(new BigDecimal(count)).divide(new BigDecimal(5),BigDecimal.ROUND_HALF_UP).toString();
        score.setOdi(odi);

        System.out.println("==========================计算VAS==========================");
        String vas = hurtPatientQuestionnaire.getQuestion11();
        score.setVas(vas);

        int vasScore = Integer.parseInt(vas);
        if (vasScore >= 0 && vasScore <= 1) {
            score.setVasGrade("好");
        } else if (vasScore >= 2 && vasScore <= 3) {
            score.setVasGrade("良");
        } else if (vasScore >= 4 && vasScore <= 6) {
            score.setVasGrade("中");
        } else if (vasScore >= 7 && vasScore <= 10) {
            score.setVasGrade("差");
        }

        System.out.println("==========================计算标准化得分==========================");
        BigDecimal four=new BigDecimal(4);
        BigDecimal eight=new BigDecimal(8);
        BigDecimal hundred=new BigDecimal(100);

        //GH=(a1-1)/4*100
        int a1=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion12())){
            a1 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion12());
        }
        BigDecimal GH = new BigDecimal(a1-1).multiply(hundred).divide(four,BigDecimal.ROUND_HALF_UP);
        //GHZ = (GH - 72.19795) / 23.19041
        BigDecimal GHZ = (GH.subtract(BigDecimal.valueOf(72.19795))).divide(BigDecimal.valueOf(23.19041),BigDecimal.ROUND_HALF_UP);
        score.setGhz(String.valueOf(GHZ));

        //PF=(a2+a3-2)/4*100
        int a2=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion13())){
            a2 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion13());
        }
        int a3=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion14())){
            a3 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion14());
        }
        BigDecimal PF = new BigDecimal(a2 + a3 - 2).multiply(hundred).divide(four,BigDecimal.ROUND_HALF_UP);
        //PFZ = (PF - 81.18122) / 29.10588
        BigDecimal PFZ = (PF.subtract(BigDecimal.valueOf(81.18122))).divide(BigDecimal.valueOf(29.10588),BigDecimal.ROUND_HALF_UP);
        score.setPfz(String.valueOf(PFZ));

        //RP=(a4+a5-2)/8*100
        int a4=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion15())){
            a4 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion15());
        }
        int a5=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion16())){
            a5 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion16());
        }
        BigDecimal RP = new BigDecimal(a4 + a5 - 2).multiply(hundred).divide(eight,BigDecimal.ROUND_HALF_UP);
        //RPZ = (RP - 80.52856) / 27.13526
        BigDecimal RPZ = (RP.subtract(BigDecimal.valueOf(80.52856))).divide(BigDecimal.valueOf(27.13526),BigDecimal.ROUND_HALF_UP);
        score.setRpz(String.valueOf(RPZ));

        //BP=(a8-1)/4*100
        int a8=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion19())){
            a8 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion19());
        }
        BigDecimal BP = new BigDecimal(a8 - 1).multiply(hundred).divide(four,BigDecimal.ROUND_HALF_UP);
        //BPZ = (BP - 81.74015) / 24.53019
        BigDecimal BPZ = (BP.subtract(BigDecimal.valueOf(81.74015))).divide(BigDecimal.valueOf(24.53019),BigDecimal.ROUND_HALF_UP);
        score.setBpz(String.valueOf(BPZ));

        //RE=(a6+a7-2)/8*100
        int a6=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion17())){
            a6 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion17());
        }
        int a7=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion18())){
            a7 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion18());
        }
        BigDecimal RE = new BigDecimal(a6 + a7 - 2).multiply(hundred).divide(eight,BigDecimal.ROUND_HALF_UP);
        //REZ = (RE - 86.41051) / 22.35543
        BigDecimal REZ = (RE.subtract(BigDecimal.valueOf(86.41051))).divide(BigDecimal.valueOf(22.35543),BigDecimal.ROUND_HALF_UP);
        score.setRez(String.valueOf(REZ));

        //MH=(a9+a11-2)/8*100
        int a9=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion20())){
            a9 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion20());
        }
        int a11=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion22())){
            a11 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion22());
        }
        BigDecimal MH = new BigDecimal(a9 + a11 - 2).multiply(hundred).divide(eight,BigDecimal.ROUND_HALF_UP);
        //MHZ = (MH - 70.18217) / 20.50597
        BigDecimal MHZ = (MH.subtract(BigDecimal.valueOf(70.18217))).divide(BigDecimal.valueOf(20.50597),BigDecimal.ROUND_HALF_UP);
        score.setMhz(String.valueOf(MHZ));

        //VT=(a10-1)/4*100
        int a10=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion21())){
            a10 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion21());
        }
        BigDecimal VT = new BigDecimal(a10 - 1).multiply(hundred).divide(four,BigDecimal.ROUND_HALF_UP);
        //VTZ = (VT - 55.59090) / 24.84380
        BigDecimal VTZ = (VT.subtract(BigDecimal.valueOf(55.59090))).divide(BigDecimal.valueOf(24.84380),BigDecimal.ROUND_HALF_UP);
        score.setVtz(String.valueOf(VTZ));

        //SF=(a12-1)/4*100
        int a12=0;
        if (Strings.isNotBlank(hurtPatientQuestionnaire.getQuestion23())){
            a12 = Integer.parseInt(hurtPatientQuestionnaire.getQuestion23());
        }
        BigDecimal SF = new BigDecimal(a12 - 1).multiply(hundred).divide(four,BigDecimal.ROUND_HALF_UP);
        //SFZ = (SF - 83.73973) / 24.75775
        BigDecimal SFZ = (SF.subtract(BigDecimal.valueOf(83.73973))).divide(BigDecimal.valueOf(24.75775),BigDecimal.ROUND_HALF_UP);
        score.setSfz(String.valueOf(SFZ));

        System.out.println("==========================计算PCS==========================");
        DecimalFormat decimalFormat = new DecimalFormat("#.#####");
        /*
        躯体健康得分 PCS=[ (GHZ * 0.24954) + (PFZ * 0.42402) + (RPZ * 0.35119) +
        (REZ * -0.19206) + (BPZ * 0.31754) + (MHZ *-0.22069) + (VTZ * 0.02877) + (SFZ * -0.00753) ]*10+50
        */
        BigDecimal pcs1 = GHZ.multiply(BigDecimal.valueOf(0.24954));
        BigDecimal pcs2 = PFZ.multiply(BigDecimal.valueOf(0.42402));
        BigDecimal pcs3 = RPZ.multiply(BigDecimal.valueOf(0.35119));
        BigDecimal pcs4 = REZ.multiply(BigDecimal.valueOf(-0.19206));
        BigDecimal pcs5 = BPZ.multiply(BigDecimal.valueOf(0.31754));
        BigDecimal pcs6 = MHZ.multiply(BigDecimal.valueOf(-0.22069));
        BigDecimal pcs7 = VTZ.multiply(BigDecimal.valueOf(0.02877));
        BigDecimal pcs8 = SFZ.multiply(BigDecimal.valueOf(-0.00753));
        BigDecimal PCS = (pcs1.add(pcs2).add(pcs3).add(pcs4).add(pcs5).add(pcs6).add(pcs7).add(pcs8)).multiply(BigDecimal.valueOf(10)).add(BigDecimal.valueOf(50));
        score.setPcs(decimalFormat.format(PCS));

        System.out.println("==========================计算MCS==========================");
        /*
        心理健康得分 MCS=[ (GHZ * -0.01571) + (PFZ * -0.22999) + (RPZ * -0.12329)
        + (REZ * 0.43407) + (BPZ * -0.09731) + (MHZ * 0.48581) + (VTZ * 0.23534) + (SFZ * 0.26876) ]*10+50
        */
        BigDecimal mcs1 = GHZ.multiply(BigDecimal.valueOf(-0.01571));
        BigDecimal mcs2 = PFZ.multiply(BigDecimal.valueOf(-0.22999));
        BigDecimal mcs3 = RPZ.multiply(BigDecimal.valueOf(-0.12329));
        BigDecimal mcs4 = REZ.multiply(BigDecimal.valueOf(0.43407));
        BigDecimal mcs5 = BPZ.multiply(BigDecimal.valueOf(-0.09731));
        BigDecimal mcs6 = MHZ.multiply(BigDecimal.valueOf(0.48581));
        BigDecimal mcs7 = VTZ.multiply(BigDecimal.valueOf(0.23534));
        BigDecimal mcs8 = SFZ.multiply(BigDecimal.valueOf(0.26876));
        BigDecimal MCS = (mcs1.add(mcs2).add(mcs3).add(mcs4).add(mcs5).add(mcs6).add(mcs7).add(mcs8)).multiply(BigDecimal.valueOf(10)).add(BigDecimal.valueOf(50));
        score.setMcs(decimalFormat.format(MCS));

        hurtQuestionnaireScoreService.insertHurtQuestionnaireScore(score);
    }
}
